package Controller.User;

import Controller.MainState.MainState;
import Controller.MainState.StudentUserState;
import Dao.LoginAndExitDao;
import Dao.StudentDao;
import DataClass.LoginAndExit;
import DataClass.Student;
import Util.TimeUtil;

import java.sql.SQLException;

/**
 * @since 2024-06-06
 * @author yooo_fan
 */
public class StudentUser implements User {
    private String ID;
    private String PASSWORD;
    private LoginAndExit log = new LoginAndExit();


    private Student DetailInformation = null;

    public StudentUser(String ID, String PASSWORD) {
        this.ID = ID;
        this.PASSWORD = PASSWORD;
    }

    @Override
    public String getId() {
        return this.ID;
    }

    @Override
    public String getPassword() {
        return this.PASSWORD;
    }

    @Override
    public Boolean tryToLogin(String account) {

        try{
            this.DetailInformation = StudentDao.getStudent(account);
        }catch (SQLException e){
            return false;
        }

        log.setRUid(account);
        log.setRPName(this.DetailInformation.getSCName());
        log.setInfoInTime(TimeUtil.getCurrentTimestamp());
        log.setInfoType("ST");
        log.setRUTeacher(this.DetailInformation.getSTNo());
        log.setRUAnoTeacher(this.DetailInformation.getSATNo());
        return true;
    }

    @Override
    public void Logout() {
        log.setInfoOutTime(TimeUtil.getCurrentTimestamp());
        System.out.println(this.getUsername()+"登出");
        try {
            LoginAndExitDao.insertLoginAndExitData(log);
        }catch (SQLException e){
            System.err.println("登录信息登记失败"+e.getMessage());
        }
    }

    @Override
    public UserType getUSerType() {
        return UserType.STUDENT;
    }

    @Override
    public Student getDetailInformation() {
        return this.DetailInformation;
    }

    @Override
    public MainState getMainState() {
        System.out.println("将mainState设置为student");
        return new StudentUserState();
    }

    @Override
    public String getUsername() {
        return this.DetailInformation.getSCName();
    }
}